/*Author: Seungbeom Ma
 *SJSU 
 * DATE: NOV 16
 * */

// Handling db communication between device and cloud
package tool.http.networks;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import tool.workflow.Graph;

import android.util.Log;

public class DataCommunication {
	private Graph g = new Graph();
	// sending data to cloud
	//@parm  NetWorkOpt = 1 : sending to real cloud
	//				      2 : Sending to AVD which is local private cloud
	// DB_Packet       : String type of packet which is based on TASK_ID;Amount_Process
	//@return; String type : Task_ID:AVG GUESS FUNCTION reuslt
	public String post(int NetWorkOpt , String DB_Packet){
		String re_val = "";
		String http_address = "";
		if(NetWorkOpt == 2){
			
			http_address = "http://windmsb.appspot.com/sign";
		}
		else if (NetWorkOpt == 1){
		//http_address = "http://windmsb.appspot.com/sign";
			http_address = "http://1.rex19881213.appspot.com/sign";
		}
		else{
			// will check to cloud later
			// http_address = "http://windmsb.appspot.com/sign";
			http_address = "http://10.0.2.2:8888/sign";
		}
		// Create Http session
		HttpClient httpClient = new DefaultHttpClient();
		// Set Address
		Log.d("Url is " , http_address);
		HttpPost httpPost = new HttpPost(http_address);
		// Wrap all packets
		List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
		nameValuePair.add(new BasicNameValuePair("content", DB_Packet));
		nameValuePair.add(new BasicNameValuePair("guestbookName", "default"));
		
		// Url Encoding the POST parameters
		try {
			// request packets
		    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
		}
		catch (UnsupportedEncodingException e) {
		    // writing error to Log
		    e.printStackTrace();
		}
		// Making HTTP Request
		try {
			// Receive packets
		    HttpResponse response = httpClient.execute(httpPost);
		 
		    // writing response to log
//		    Log.d("Http Response:", response.toString());
		 
		   // System.out.println("Http Response:" + EntityUtils.toString(response.getEntity(), "UTF-8"));
		    // convert to string type packets with UTF-8 Encoding
		    re_val = EntityUtils.toString(response.getEntity(), "UTF-8");
		   // HttpEntity entity = response.getEntity();
		 
		} catch (ClientProtocolException e) {
		    // writing exception to log
		    e.printStackTrace();
		         
		} catch (IOException e) {
		    // writing exception to log
		    e.printStackTrace();
		}
		System.out.println("post Reruning" + re_val);
		 return re_val;
	}
	// building packet for cloud 
	public String PacketBuilder(Graph Workflow){
		ArrayList<String> Workflow_list = Workflow.bfs();
		return g.GraphToString(Workflow_list);
	}
	
	public ArrayList<String> Double_Pucket_Builder(Graph Workflow){
		ArrayList<String> re_list= new ArrayList<String>();
		
		ArrayList<ArrayList<String>> Cloud_AND_Local = new ArrayList<ArrayList<String>>();
		Cloud_AND_Local = Workflow.SJSU_bfs();
		re_list.add( g.GraphToString(Cloud_AND_Local.get(0)));
		re_list.add( g.GraphToString(Cloud_AND_Local.get(1)));
		re_list.add( g.GraphToString(Cloud_AND_Local.get(2)));
		return re_list;
  }
	
    

}
